package cn.cs.yangtai.service.impl;

import cn.cs.yangtai.entity.Comminicate;
import cn.cs.yangtai.entity.Expert;
import cn.cs.yangtai.entity.vo.PageResult;
import cn.cs.yangtai.mapper.ExpertMapper;
import cn.cs.yangtai.service.ExpertService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 专家表(Expert)表服务实现类
 *
 * @author makejava
 * @since 2024-12-17 13:11:18
 */
@Service("expertService")
public class ExpertServiceImpl implements ExpertService {
    @Resource
    private ExpertMapper expertMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param eXpertId 主键
     * @return 实例对象
     */
    @Override
    public Expert queryById(Integer eXpertId) {
        return this.expertMapper.queryById(eXpertId);
    }

    /**
     * 分页查询
     *
     * @param cuttentPage 本页
     * @param pageSize    分页大小
     * @return 查询结果
     */
    @Override
    public PageResult<Expert> queryByPage(Integer cuttentPage, Integer pageSize) {
        PageHelper.startPage(cuttentPage,pageSize);
        Page<Expert> experts =expertMapper.queryAllByLimit(new Page<>());
        return PageResult.restPage(experts);
    }

    /**
     * 新增数据
     *
     * @param expert 实例对象
     * @return 实例对象
     */
    @Override
    public Expert insert(Expert expert) {
        this.expertMapper.insert(expert);
        return expert;
    }

    /**
     * 修改数据
     *
     * @param expert 实例对象
     * @return 实例对象
     */
    @Override
    public Expert update(Expert expert) {
        this.expertMapper.update(expert);
        return this.queryById(expert.getEXpertId());
    }

    /**
     * 通过主键删除数据
     *
     * @param eXpertId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer eXpertId) {
        return this.expertMapper.deleteById(eXpertId) > 0;
    }

    /**
     * 根据条件查询
     * @param expert
     * @return
     */
    @Override
    public List<Expert> queryByCondition(Expert expert) {
        return expertMapper.queryByCondition(expert);
    }

    /**
     *
     * @param field
     * @return
     */

    @Override
    public List<Expert> recommend(String field) {
        List<Expert> experts = expertMapper.recommend(field);
        List<Expert> standings = new ArrayList<>(experts);
        //如果结果集大于10，则推荐前10个，否则推荐全部
        if(experts.size()>0){
           if(experts.size()>10){
               Collections.shuffle(standings);
               standings.subList(0,10);
           }else {
               Collections.shuffle(standings);
               standings.subList(0,experts.size());
           }
        }else {
            throw new RuntimeException("没有找到相关专家");
        }
        return standings;
    }
}
